package com.qfedu.Action;

import com.qfedu.biz.AddressBiz;
import com.qfedu.biz.CustomerBiz;
import com.qfedu.entity.AddressEntity;
import com.qfedu.entity.CustomerEntity;
import com.qfedu.util.Ajaxd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AddressAction {
    @Autowired
    private AddressBiz addressBiz;
    @Autowired
    private CustomerBiz customerBiz;
    @RequestMapping("/queryAdById.do")
    @ResponseBody
    public AddressEntity doqueryAd(String addressid, HttpServletRequest request){
    int aid=Integer.parseInt(addressid);
        AddressEntity addressEntity = addressBiz.queryAdById(aid);

        return addressEntity;
    }

    @RequestMapping("/updateAddress.do")
    @ResponseBody
    public Ajaxd doupdate(String cname,String cphone,AddressEntity addressEntity,HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        HttpSession session = request.getSession();
        CustomerEntity customerinfo = (CustomerEntity)session.getAttribute("customerinfo");
        customerinfo.setCname(cname);
        customerinfo.setCphone(cphone);
        Boolean aBoolean1 = customerBiz.updateCus(customerinfo);
        Boolean aBoolean = addressBiz.updateAddress(addressEntity);
        if (aBoolean&&aBoolean1){
            ajaxd.setFlag(true);
        }else{
            ajaxd.setFlag(false);
        }

        return ajaxd;
    }



    @RequestMapping("/insertAddress.do")
    @ResponseBody
    public List<AddressEntity> doinsertAddress(AddressEntity addressEntity){
        Boolean aBoolean = addressBiz.insertAddress(addressEntity);
        addressEntity.setAddressid(0);
        List<AddressEntity> addressEntities = addressBiz.queryAddressByCid(addressEntity);
        return addressEntities;
    }
}
